package com.qf.service;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.qf.base.service.impl.BaseServiceImpl;
import com.qf.entity.User;
import com.qf.mapper.IUserMapper;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description:
 * @Author: xbb
 * @Date:2020/4/14
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private IUserMapper userMapper;

    @Override
    public BaseMapper<User> getMapper() {
        return userMapper;
    }

    @Override
    public Integer register(User user) {
        //1.判断用户是否存在
        if (!isExist("username",user.getUsername())){
            return 1;   //用户名已存在
        } else if (!isExist("phone",user.getPhone())){
            return 2;//手机号已被注册
        } else {
            userMapper.insert(user);
            return 3;
        }
    }

    @Override
    public User getUserByName(String username) {
        User user = new User();
        user.setUsername(username);

        return userMapper.selectOne(user);
    }

    //判断用户是否存在
    public Boolean isExist(String column,String value){
        EntityWrapper wrapper = new EntityWrapper();
        wrapper.eq(column,value);
        return userMapper.selectCount(wrapper)==0;
    }
}
